﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Hospitals.Procurement
{
    public partial class PurchaseOrderCopy : BasePage
    {
        IBLL_Purchase purchaseCtrl = Factory.BLL_Purchase();

        //public PurchaseOrderCopy()
        //{
        //    this.ModuleNo = Module.采购单管理.ToString();
        //    this.PageOperateType = OperateType.新增.ToString();
        //}

        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request.QueryString["OrderID"]))
            {
                PageError("参数错误！","");
            }

            if (!IsPostBack)
            {
                //绑定默认的采购单名称：所在机构名+时间+"采购单"
                txtPurchaseOrderName.Text = CurrentAccount.GetUser().Org.OrgName + DateTime.Now.ToString("yyyyMMddHHmm") + "采购单";
            }
        }


        /// <summary>
        /// 保存新采购单，并将原采购的信息复制到新采购单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //检查数据
            if (!string.IsNullOrEmpty(DataCheck()))
            {
                string errorMsg = DataCheck();
                HZ.Web.WebUtil.Alert(errorMsg, this);
                return;
            }

            //原采购单ID
            string oldOrderID= Request.QueryString["OrderID"].Trim();  

            //新采购单信息
            PurchaseOrder aPurchaseOrder = new PurchaseOrder();
            aPurchaseOrder.OrderID = Guid.NewGuid().ToString();
            aPurchaseOrder.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            aPurchaseOrder.OrderName = txtPurchaseOrderName.Text.Trim();
            aPurchaseOrder.StartTime = Convert.ToDateTime(txtPurchaseOrderTimeFrom.Text.Trim());
            aPurchaseOrder.PlanEndTime = Convert.ToDateTime(txtPurchaseOrderTimeTo.Text.Trim());
            aPurchaseOrder.PlanPerson = CurrentAccount.GetUser().UserID;
            aPurchaseOrder.TotalAmount = 0;
            aPurchaseOrder.Remarks = txtRemarks.Text;
            string message = string.Empty;
            int flag = purchaseCtrl.CopyPurchaseOrder(aPurchaseOrder, oldOrderID,out message);
            if (flag == 1)
            {
                PageInfo("采购单复制成功"+message+"，请返回列表查看或继续编辑！", "PurchaseOrderUnCheck.aspx");
            }
            else
            {
                PageError("操作失败！", this.ReturnUrl);
            }
        }

        /// <summary>
        /// 检查数据合法性的方法
        /// </summary>
        /// <returns></returns>
        private string DataCheck()
        {
            string errorMsg = "";
            //采购单名称为空
            if (string.IsNullOrEmpty(txtPurchaseOrderName.Text.Trim()))
            {
                errorMsg = "采购单名称不能为空！";
                txtPurchaseOrderName.Focus();
                return errorMsg;
            }

            //采购时间不能为空
            if (string.IsNullOrEmpty(txtPurchaseOrderTimeFrom.Text.Trim()) || string.IsNullOrEmpty(txtPurchaseOrderTimeTo.Text.Trim()))
            {
                errorMsg = "采购时间不能为空！";
                txtPurchaseOrderTimeFrom.Focus();
                return errorMsg;
            }

            //采购开始时间不得早于现在的时间
            if (Convert.ToDateTime(txtPurchaseOrderTimeFrom.Text.Trim()).Date < DateTime.Now.Date)
            {
                errorMsg = "采购开始时间不得晚于当前时间";
                txtPurchaseOrderTimeFrom.Focus();
                return errorMsg;
            }

            //采购开始时间晚于结束时间
            if (Convert.ToDateTime(txtPurchaseOrderTimeFrom.Text.Trim()) > Convert.ToDateTime(txtPurchaseOrderTimeTo.Text.Trim()))
            {
                errorMsg = "采购开始时间晚于结束时间！";
                txtPurchaseOrderTimeFrom.Focus();
                return errorMsg;
            }
            return errorMsg;

        }


    }
}
